package com.gdufe.firesafe.controller;

import com.gdufe.firesafe.annotation.AuthAccess;
import com.gdufe.firesafe.annotation.NeedAdmin;
import com.gdufe.firesafe.annotation.NeedInspector;
import com.gdufe.firesafe.annotation.NeedOwner;
import com.gdufe.firesafe.model.entity.UserEntity;
import com.gdufe.firesafe.utils.PageUtils;
import com.gdufe.firesafe.utils.R;
import com.gdufe.firesafe.model.entity.HouseEntity;
import com.gdufe.firesafe.service.HouseService;
import com.gdufe.firesafe.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 房屋表
 *
 * @author Liu
 * @email
 * @date 2024-03-26 16:29:57
 */
@RestController
@RequestMapping("house/house")
public class HouseController {
    @Autowired
    private HouseService houseService;

    /**
     * 获取房屋安全情况统计数据
     */
    @NeedOwner
    @GetMapping("/getSafeStatistics")
    public R getSafeStatistics(){
        UserEntity currentUser = TokenUtils.getCurrentUser();
        return R.ok().put("data",houseService.getSafeStatistics(currentUser.getRoleId()));
    }

    /**
     *获取房屋基本信息
     */
    @NeedOwner
    @GetMapping("/getBasicInfo")
    public R getBasicInfo(){
        UserEntity currentUser = TokenUtils.getCurrentUser();
        return R.ok().put("data",houseService.getBasicInfo(currentUser.getRoleId()));
    }

    /**
     * 获取业主分数
     */
    @NeedOwner
    @GetMapping("/getOwnerPoint/{ownerId}")
    public R getOwnerPoint(@PathVariable("ownerId") Integer ownerId){
        return R.ok().put("data",houseService.getOwnerPoint(ownerId));
    }

    /**
     * 获取房屋名
     */
    @GetMapping("/getHouseNameById/{id}")
    public R getHouseNameById(@PathVariable("id") Integer id){
        return R.ok().put("data",houseService.getHouseNameById(id));
    }

    /**
     * 获取房屋树形结构
     */
    @AuthAccess
    @GetMapping("/getHouseTree")
    public R getHouseTree(){
        return R.ok().put("data", houseService.getHouseTree());
    }

    /**
     * 检查员的负责对象列表
     */
    @NeedInspector
    @GetMapping("/getInspectorHouseList")
    public R getInspectorHouseList(@RequestParam Map<String, Object> params){
        UserEntity currentUser = TokenUtils.getCurrentUser();
        return R.ok().put("page",houseService.getInspectorHouseList(params,currentUser.getRoleId()));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = houseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		HouseEntity house = houseService.getById(id);

        return R.ok().put("house", house);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody HouseEntity house){
		houseService.save(house);

        return R.ok();
    }

    /**
     * 修改
     */
    @NeedAdmin
    @RequestMapping("/update")
    public R update(@RequestBody HouseEntity house){
		houseService.updateById(house);

        return R.ok();
    }

    /**
     * 删除
     */
    @NeedAdmin
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		houseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
